package com.qian.mapper;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.qian.pojo.Employees;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface EmployeeMapper extends BaseMapper<Employees> {

    //此Mapper中均使用注解方式实现增删改查操作,目的是避开mybatis-plus中对多租户的检查机制，实现过滤操作

    //过滤操作,登录功能不需要判断多租户条件
    @SqlParser(filter = true)
    @Select("SELECT * FROM employees where employee_account = #{employee_account}")
    List<Employees> employeeLogin(@Param("employee_account") String employeeAccount);

    //查询所有租户
    @SqlParser(filter = true)
    @Select("SELECT * FROM employees where authority = #{authority} and employee_name like CONCAT('%',#{employee_name},'%')")
    List<Employees> tenantAll(@Param("authority") int authority, @Param("employee_name") String employee_name);

    //新增租户
    @SqlParser(filter = true)
    @Insert("INSERT INTO employees (employee_id,employee_account,employee_password,employee_name,employee_company,employee_gender,employee_email,employee_address,employee_position,authority,tenant_Id) " +
            "values (#{employeeId},#{employeeAccount},#{employeePassword},#{employeeName},#{employeeCompany},#{employeeGender},#{employeeEmail},#{employeeAddress},#{employeePosition},#{Authority},#{tenantId})")
    void addTenant(Employees employees);

    //获取当前租户号的最大值
    @SqlParser(filter = true)
    @Select("SELECT MAX(tenant_id) FROM employees")
    int maxTenant();

    //通过id查询租户
    @SqlParser(filter = true)
    @Select("SELECT * FROM employees where employee_id = #{employee_id}")
    Employees queryTenantById(@Param("employee_id") Long employee_id);

    //更新租户信息
    @SqlParser(filter = true)
    @Update("UPDATE employees SET employee_password = #{employeePassword},employee_name = #{employeeName},employee_gender=#{employeeGender}," +
            "employee_company = #{employeeCompany},employee_email = #{employeeEmail},employee_address=#{employeeAddress},employee_position=#{employeePosition}" +
            " where employee_id = #{employeeId}")
    void updateTenant(Employees employees);

    //删除租户信息
    @SqlParser(filter = true)
    @Delete("DELETE FROM employees where employee_id = #{employee_id}")
    void deleteTenant(@Param("employee_id") Long employee_id);

    //获取当前所有的租户号
    @SqlParser(filter = true)
    @Select("SELECT DISTINCT tenant_id FROM employees")
    List<Long> allTenants();
}
